
package com.hlkj.pay.util;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import cn.hutool.jwt.RegisteredPayload;
import com.hlkj.pay.enums.AuthTokenEnum;
import com.hlkj.pay.vo.common.resp.token.TokenInfoResp;

import java.nio.charset.StandardCharsets;

/**
 * @author HlpayTeam
 * @date 2024/09/27 18:02
 */
// @Log4j2
public class TokenUtils {

    /**
     * parseToken
     * 
     * @param token
     * @return
     */
    public static boolean checkToken(String token, String key) {
        try {
            return JWTUtil.verify(token, key.getBytes(StandardCharsets.UTF_8));
        }
        catch (Exception e) {
            // log.error("checkToken token:{},fail:{}", token, e.getMessage());
            return false;
        }
    }

    /**
     * parseToken
     * 
     * @param token
     * @return
     */
    public static TokenInfoResp parseToken(String token) {
        TokenInfoResp tokenInfoDTO = new TokenInfoResp();
        try {
            JWT jwt = JWTUtil.parseToken(token);
            if (jwt != null) {
                Object userId = jwt.getPayload(AuthTokenEnum.AUTH_TOKEN_FIELD.USER_ID.getCode());
                Object issuedTime = jwt.getPayload(RegisteredPayload.ISSUED_AT);
                Object expireTime = jwt.getPayload(RegisteredPayload.EXPIRES_AT);
                Object appId = jwt.getPayload(AuthTokenEnum.AUTH_TOKEN_FIELD.APP_ID.getCode());
                Object sysType = jwt.getPayload(AuthTokenEnum.AUTH_TOKEN_FIELD.SYS_TYPE.getCode());
                Object authTokenType = jwt.getPayload(AuthTokenEnum.AUTH_TOKEN_FIELD.AUTH_TOKEN_TYPE.getCode());
                Object sn = jwt.getPayload(AuthTokenEnum.AUTH_TOKEN_FIELD.SN.getCode());
                // tokenInfoDTO.setTenantId(tenantId.toString());
                tokenInfoDTO.setAppId(appId.toString());
                if (userId != null) {
                    tokenInfoDTO.setUserId(Long.parseLong(userId.toString()));
                }
                if (authTokenType != null) {
                    tokenInfoDTO.setAuthTokenType(Integer.parseInt(authTokenType.toString()));
                }
                if (sysType != null) {
                    tokenInfoDTO.setSysType(Integer.parseInt(sysType.toString()));
                }
                if (sn != null) {
                    tokenInfoDTO.setSn(sn.toString());
                }
                tokenInfoDTO.setExpireTime(Long.parseLong(expireTime.toString()));
                tokenInfoDTO.setIssuedTime(Long.parseLong(issuedTime.toString()));
                return tokenInfoDTO;
            }
        }
        catch (Exception e) {
            // log.error("parseToken token:{},fail:{}", token, e.getMessage());
        }
        return null;
    }
    public static void main(String[] args) {
        String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9pcGVkOWp4bS55aGczZ2Z5ZnNwdHdwYjl5Lmp0cDEwMDYudG9wXC9hcHBcL2F1dGhcLzQwODcxNTEiLCJpYXQiOjE2OTgzNzE5NDMsImV4cCI6MTY5ODU0NDc0MywibmJmIjoxNjk4MzcxOTQzLCJqdGkiOiIzOWZ3SjVQRlpoMEZpODBNIiwic3ViIjo0MDg3MTUxLCJwcnYiOiI1ODkzZWZlMTc4ZmJiNGVhZmI5ODRkMzE5NjA3N2MzNDQxMjI0MzYzIiwiYXBwaWQiOiJ3eGNhN2RhMzEzNjdiNGYyMDIiLCJndWFyZCI6ImFwcCJ9.rJBjOGafm5DpUNdUQJ6JgjFnWIxD3fbkpUgIAVIUBvw";
        System.out.println(checkToken(token,""));
    }
}
